package cn.xshi.job.web;

import cn.xshi.common.annotation.Auth;
import cn.xshi.common.base.BaseAction;
import cn.xshi.common.base.BasePage;
import cn.xshi.common.base.BaseResult;
import cn.xshi.common.entity.OauthAccountEntity;
import cn.xshi.common.util.StringUtil;
import cn.xshi.job.form.JobConfigForm;
import cn.xshi.job.model.JobConfig;
import cn.xshi.job.service.JobConfigService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Desc 任务调度配置
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@RestController
@RequestMapping("/jobConfig")
@Api(value = "任务调度配置API",tags = "任务调度配置API",description = "任务调度配置API")
public class JobConfigController extends BaseAction {

	@Resource
	private JobConfigService jobConfigService;

	/**
	 * 加载初始化列表数据并分页
	 * @param jobConfigForm
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@ApiOperation(value=" 查询并分页", notes=" 查询并分页")
	@GetMapping(value="/list")
	@Auth(value = "/jobConfig/list",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	public BasePage<List<JobConfig>> getJobConfigListByCondition(JobConfigForm jobConfigForm, Integer pageNo, Integer pageSize){
		commonHPagerByPageNo(pageNo,pageSize);
		List<JobConfig> jobConfigList = jobConfigService.getJobConfigListByCondition(jobConfigForm);
		PageInfo<JobConfig> page = new PageInfo<JobConfig>(jobConfigList);
		return outPageBootByPageNo(page,pageNo,pageSize);
	}

	/**
	 *
	 * @return
	 */
	@ApiOperation(value=" 查询集合", notes=" 查询集合")
	@GetMapping(value="/lists")
	@Auth(value = "/jobConfig/lists",authenticationType = Auth.AuthorizationType.NOT_REQUIRED_LOGIN)
	public BaseResult<List<JobConfig>> lists(){
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("jobStatus", "NORMAL");
		List<JobConfig> jobConfigList = jobConfigService.getJobConfigListAllByCondition(condition);
		return outDataStr(jobConfigList);
	}
	
	/**
	* 单个记录
	* @param id
	*/
	@GetMapping(value="/get/{id}")
	@Auth(value = "/jobConfig/get",authenticationType = Auth.AuthorizationType.REQUIRED_LOGIN_NOT_AUTH)
	@ApiOperation(value=" 查询单个记录", notes=" 查询单个记录")
	public BaseResult<JobConfig> getJobConfigById(@PathVariable("id")String id){
		JobConfig jobConfig = jobConfigService.getJobConfigById(id);
		if(!StringUtil.isEmpty(jobConfig.getCreateId())){
			OauthAccountEntity createBy = getAccount(jobConfig.getCreateId());
			if(null != createBy){
				jobConfig.setCreateBy(createBy.getName());
			}
		}
		if(!StringUtil.isEmpty(jobConfig.getUpdateId())){
			OauthAccountEntity modifiedBy = getAccount(jobConfig.getUpdateId());
			if(null != modifiedBy){
				jobConfig.setModifiedBy(modifiedBy.getName());
			}
		}
		return outDataStr(jobConfig);
	}
	
	/**
	* 添加
	* @param jobConfig
	*/
	@ApiOperation(value=" 添加", notes=" 添加")
	@PostMapping(value="/add")
	@Auth(value = "/jobConfig/add")
	public BaseResult addJobConfig(@RequestBody JobConfig jobConfig){
		int i = 0;
		if(null != jobConfig){
			jobConfig.setId(toUUID());
			i= jobConfigService.addJobConfig(jobConfig);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 修改
	* @param jobConfig
	*/
	@ApiOperation(value=" 修改", notes=" 修改")
	@PutMapping(value="/update")
	public BaseResult updateJobConfig(@RequestBody JobConfig jobConfig){
		int i = 0;
		if(null != jobConfig){
			i= jobConfigService.updateJobConfig(jobConfig);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
	
	/**
	* 删除
	* @param id
	*/
	@ApiOperation(value="删除", notes=" 删除")
	@DeleteMapping(value="/delete")
	public BaseResult delJobConfig(String id){
		int i = 0;
		if(!StringUtil.isEmpty(id)){
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("id",id.split(","));
			i= jobConfigService.delJobConfig(condition);
		}
		if(i>0){
			return outAudStr(true);
		}else{
			return outAudStr(false);
		}
	}
}
